package genalgoritm.viewer;

import genalgoritm.count.RealChromosome;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

public class PersonEditingSupport extends EditingSupport {
	private CellEditor editor;
	private int column;

	public PersonEditingSupport(ColumnViewer viewer, int column) {
		super(viewer);

		// Create the correct editor based on the column index
		switch (column) {
		case 0:
			editor = new TextCellEditor(((TableViewer) viewer).getTable());
			break;
		}
		this.column = column;
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		RealChromosome person = (RealChromosome) element;

		switch (this.column) {
		case 0:
			return String.valueOf(person.getX());
		default:
			break;
		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		RealChromosome pers = (RealChromosome) element;

		switch (this.column) {
		case 0:
			try {
				pers.setX(Double.valueOf(String.valueOf(value)));
				getViewer().refresh();
			} catch (NumberFormatException e) {
			}
			break;
		default:
			break;
		}

		getViewer().update(element, null);
	}

	public CellEditor getEditor() {
		return editor;
	}
}
